package com.tcm.portal.web.controller;

import com.base.core.exception.ServiceException;
import com.base.core.pager.PageBean;
import com.base.core.utils.R;
import com.tcm.consultation.dto.ConsultationDTO;
import com.tcm.consultation.dto.PageConsultationDTO;
import com.tcm.consultation.service.UserConsultationService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/portal")
@RestController
public class ConsultationController {

    @DubboReference(version = "0.0.1-alpha", timeout = 6000, check = false, retries = 3)
    private UserConsultationService userConsultationService;

    @GetMapping("/consult/detail")
    public R view(@RequestParam("id") Long id) {
       return R.success(userConsultationService.findConsultationById(id));
    }

    /**
     * 获取问诊单列表
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/consult/list/page")
    public R listPage(@RequestParam Long userId,
                      @RequestParam(defaultValue = "1") Long page,
                      @RequestParam(defaultValue = "10") Long pagesize) {
        try{
            PageBean<PageConsultationDTO> consultationDTOPageBean;
            consultationDTOPageBean = userConsultationService.listPageConsultation(userId,page,pagesize);
            return R.success(consultationDTOPageBean);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }

    }

    /**
     * 获取问诊单列表
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/consult/list/page/status")
    public R listPageByStatus(@RequestParam Long userId,
                      @RequestParam(defaultValue = "") Long patientId,
                      @RequestParam(defaultValue = "0") Byte status,
                      @RequestParam(defaultValue = "1") Long page,
                      @RequestParam(defaultValue = "10") Long pagesize) {
        try{
            PageBean<PageConsultationDTO> consultationDTOPageBean;
            consultationDTOPageBean = userConsultationService.listPageConsultation(userId,patientId, status, page,pagesize);
            return R.success(consultationDTOPageBean);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * 统计订单数
     * @param userId
     * @param status
     * @return
     */
    @GetMapping("/consult/count/status")
    public R countByStatus(@RequestParam Long userId,
                              @RequestParam(defaultValue = "") Long patientId,
                              @RequestParam Byte status) {
        try{
            Long count = userConsultationService.countConsultation(userId, patientId, status);
            return R.success(count);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * 统计全部订单数
     * @param userId
     * @param patientId
     * @return
     */
    @GetMapping("/consult/count")
    public R countConsultation(@RequestParam Long userId, @RequestParam(defaultValue = "") Long patientId) {
        try{
            Long count = userConsultationService.countConsultation(userId,patientId,null);
            return R.success(count);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * 创建订单
     * @param consultationDTO
     * @return
     */
    @PostMapping("/consult/create")
    public R create(@RequestBody ConsultationDTO consultationDTO) {
        try{
            Long id = (Long)userConsultationService.create(consultationDTO);
            return R.success(id);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @GetMapping("/consult/cancel")
    public R cancel(@RequestParam Long id) {
        try{
            userConsultationService.cancel(id);
            return R.success();
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @GetMapping("/consult/save/payment")
    public R savePayment(@RequestParam Long id, @RequestParam String payChannel) {
        try{
            userConsultationService.updatePayment(id, payChannel);
            return R.success();
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @GetMapping("/consult/delete")
    public R delete(@RequestParam Long id) {
        try{
            userConsultationService.deleteOrder(id);
            return R.success();
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }


}

